#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <zlib.h>
//python -c "print 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'*33*4" > blocks

//run failed under linux distbute
uint32_t blocks[33][2][32];

int main() {
    FILE *f = fopen("blocks", "rb");
    int len = fread(blocks, 1, 33*2*32*4, f);
    printf("%d\n", len);
    fclose(f);
    
    int *bigcrc = (int *)calloc(1LL << 32, 4);
    int *nextcrc = (int *)calloc(1LL << 32, 4);
    printf("crc initted\n");
    
    bigcrc[0] = 1;
    
    for (int j = 0; j < 32; j++) {
        printf("doing crc bullshit round %d\n", j);
        uint32_t *msg1block = blocks[j][0];
        uint32_t *msg2block = blocks[j][1];
        //printf("%s %s %d",blocks[j][0],blocks[j][1],strlen((const char *)msg1block));
        //int tmp=0;
        //scanf("%d",&tmp);
        int ccount = 0;

        //loop for 2**32
        for (long long i = 0; i < 1LL << 32; i++) {
            if (bigcrc[i] != 0) {
                ccount++;
                uint32_t nc1 = crc32(i, (unsigned char *)msg1block, 0x80);
                uint32_t nc2 = crc32(i, (unsigned char *)msg2block, 0x80);
                //printf("%d %d \n",nc1,nc2);
                // hmm i think this check is wrecking the cache
                if (nextcrc[nc1] != 0 || nextcrc[nc2] != 0) {
                    
                    int c1,c2;
                    if (nextcrc[nc1] != 0) {
                        c1 = nc1;
                        c2 = bigcrc[i]<<1;
                    } else if (nextcrc[nc2] != 0) {
                        c1 = nc2;
                        c2 = (bigcrc[i]<<1) | 1;
                    }
                    printf("FOUND COLLISION!!! %x %x %x\n", c1, nextcrc[c1], c2);
                    return 0;
                }
                nextcrc[nc1] = bigcrc[i] << 1;
                nextcrc[nc2] = (bigcrc[i] << 1) | 1;
                bigcrc[i] = 0;
            }
        }
        printf("crc count is 0x%X\n", ccount);
        
        int *tcrc = bigcrc;
        bigcrc = nextcrc;
        nextcrc = tcrc;
    }
}